package com.task.scheduling.processor.contract_processor;

import com.task.scheduling.dao.TaskSchedulingDao;
import com.task.scheduling.model.TaskResult;
import com.task.scheduling.model.TaskSchedulingModel;
import com.task.scheduling.enums.TaskStatus;
import com.task.scheduling.processor.Processor;
import com.task.scheduling.service.TaskHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 这个只是简单的把任务的data数据输出到文件当中
 * @author Administrator
 *
 */
@Component
public class FileProcessor implements Processor {

	private Logger log = LoggerFactory.getLogger(UpdateTairCacheProcessor.class);

	@Autowired
	private TaskSchedulingDao taskSchedulingDao;


	@Override
	public TaskResult process(TaskSchedulingModel model) {
		try {
			TaskHolder.holdTask(model.getId());
			System.out.println("file processor execute");



			//执行完更新任务状态
			TaskSchedulingModel upModel = new TaskSchedulingModel();
			upModel.setId(model.getId());
			upModel.setTaskStatus(TaskStatus.SUCCESS.getCode());
			upModel.setTaskMessage("任务执行的结果");
			taskSchedulingDao.updateTaskSchedule(upModel);
			return null;
		} catch (Exception e) {
			//发生错误更新任务状态
			log.error("UpdateTairCacheProcessor error " + e.getMessage(), e);
			taskSchedulingDao.updateTaskToFail(model.getId(), e.getMessage(), new Date());
			return null;
		} finally {
			TaskHolder.removeTask(model.getId());
		}

	}
}
